package pers.zxk.jlytnews.dao.impl;

import pers.zxk.jlytnews.dao.FoodMenuDao;
import pers.zxk.jlytnews.entity.FoodMenuWithFood;
import pers.zxk.jlytnews.entity.FoodWithType;
import pers.zxk.jlytnews.util.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodMenuDaoImpl implements FoodMenuDao {
    @Override
    public boolean addFoodMenu(int foodId, String date, String time, String description, String remarks) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        int i = DBconn.addUpdDel("insert into tb_foodmenu (foodid,date,time,description,remarks) " +
                "values('" + foodId + "','" + date + "','" + time + "','" + description + "','" + remarks + "')");
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean delFoodMenu(int id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from tb_foodmenu where id=" + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean updateFoodMenu(int id, int foodId, String date, String time, String description, String remarks) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        DBconn.init();
        String sql = "update tb_foodmenu set foodid ='" + foodId
                + "' , date ='" + date
                + "' , time ='" + time
                + "' , description ='" + description
                + "' , remarks ='" + remarks + "' where id = " + id;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public List<FoodMenuWithFood> getFoodMenu(String date) throws SQLException, ClassNotFoundException {
        List<FoodMenuWithFood> list = new ArrayList<FoodMenuWithFood>();

        DBconn.init();
        ResultSet rs = DBconn.selectSql("select * from tb_foodmenu left join tb_food " +
                "on tb_foodmenu.foodid = tb_food.id where tb_foodmenu.date = '" + date +
                "' order by tb_foodmenu.id ");
        while (rs.next()) {
            FoodMenuWithFood foodMenu = new FoodMenuWithFood();
            foodMenu.setId(rs.getInt("id"));
            foodMenu.setFoodId(rs.getInt("foodid"));
            foodMenu.setDate(rs.getString("date"));
            foodMenu.setTime(rs.getString("time"));
            foodMenu.setGood(rs.getInt("good"));
            foodMenu.setBad(rs.getInt("bad"));
            foodMenu.setDescription(rs.getString("description"));
            foodMenu.setRemarks(rs.getString("remarks"));
            foodMenu.setFoodName(rs.getString("tb_food.name"));
            foodMenu.setFoodImage(rs.getString("tb_food.image"));
            foodMenu.setFoodType(rs.getString("tb_food.type"));
            foodMenu.setFoodDescription(rs.getString("tb_food.description"));
            list.add(foodMenu);
        }
        DBconn.closeConn();
        return list;
    }

    @Override
    public List<FoodMenuWithFood> getFoodMenu(String date, String time) throws SQLException, ClassNotFoundException {
        List<FoodMenuWithFood> list = new ArrayList<FoodMenuWithFood>();

        DBconn.init();
        ResultSet rs = DBconn.selectSql("select * from tb_foodmenu left join tb_food " +
                "on tb_foodmenu.foodid = tb_food.id where tb_foodmenu.date = '" + date +
                "' and tb_foodmenu.time = '" + time + "' order by tb_foodmenu.id ");
        while (rs.next()) {
            FoodMenuWithFood foodMenu = new FoodMenuWithFood();
            foodMenu.setId(rs.getInt("id"));
            foodMenu.setFoodId(rs.getInt("foodid"));
            foodMenu.setDate(rs.getString("date"));
            foodMenu.setTime(rs.getString("time"));
            foodMenu.setGood(rs.getInt("good"));
            foodMenu.setBad(rs.getInt("bad"));
            foodMenu.setDescription(rs.getString("description"));
            foodMenu.setRemarks(rs.getString("remarks"));
            foodMenu.setFoodName(rs.getString("tb_food.name"));
            foodMenu.setFoodImage(rs.getString("tb_food.image"));
            foodMenu.setFoodType(rs.getString("tb_food.type"));
            foodMenu.setFoodDescription(rs.getString("tb_food.description"));
            list.add(foodMenu);
        }
        DBconn.closeConn();
        return list;
    }
}
